using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

/// <summary>
/// Ui模块管理，所有的Ui必须通过此类进行操作
/// </summary>
public class GameModuleManager : Singleton<GameModuleManager>
{
    private Dictionary<Type, GameModule> mModuleDictionary = new Dictionary<Type, GameModule>();

    /// <summary>
    /// 获取并打开Ui
    /// </summary>
    /// <typeparam name="T">new()必须具有无参构造函数，才能够正确的初始化对象</typeparam>
    /// <returns></returns>
    public T GetOpenModule<T>() where T : GameModule, new()
    {
        T module = GetModule<T>();
        if (module == null)
        {
            module = new T();
            Type type = typeof(T);
            mModuleDictionary.Add(type, module);

            //打开模块
            module.Open();
        }
        Debug.Log("打开了模块" + typeof(T));
        return module;
    }

    /// <summary>
    /// ͨ通过类型名称获取
    /// </summary>
    /// <param name="typeName"></param>
    /// <returns></returns>
    public GameModule GetOpenModule(string typeName)
    {
        Type type = Type.GetType(typeName);

        GameModule module = GetModule(type);

        if (module==null)
        {
            //穿件实例
            module = Activator.CreateInstance(type) as GameModule;
            if (module!=null)
            {
                mModuleDictionary.Add(type, module);
                module.Open();
            }
        }
        return module;
    }

    /// <summary>
    /// 关闭模块
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public void CloseModule<T>() where T : GameModule
    {
        Type type = typeof(T);
        if (mModuleDictionary.ContainsKey(type))
        {
            //关闭模块
            mModuleDictionary[type].OnClose();
            //
            mModuleDictionary.Remove(type);
        }
    }

    /// <summary>
    /// 获取模块
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetModule<T>() where T : GameModule
    {
        Type type = typeof(T);
        if (mModuleDictionary.ContainsKey(type))
        {
            return mModuleDictionary[type] as T;
        }
        return null;
    }

    /// <summary>
    /// 获取模块
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public GameModule GetModule(Type moduleType)
    {
        if (mModuleDictionary.ContainsKey(moduleType))
        {
            return mModuleDictionary[moduleType];
        }

        return null;
    }

    /// <summary>
    /// ��ȡģ��
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public GameModule GetModule(string typeName)
    {
        Type type = Type.GetType(typeName);
        return GetModule(type);
    }

    /// <summary>
    /// �������ģ��
    /// </summary>
    public void ClearModule()
    {
        foreach (KeyValuePair<Type,GameModule> item in mModuleDictionary)
        {
            if (item.Value!=null)
            {
                item.Value.OnClose();
            }
        }
        //���ȫ��ģ��
        mModuleDictionary.Clear();
    }

}
